﻿using BasicInformation.Domain;
using Contract.Domain;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;
using Trace.Infrastructure.Repository;
using Trace.Read_WebApi.Commands;

namespace Trace.Read_WebApi.CommandHandler
{
    public class ListCYTraceCommandHandler:IRequestHandler<ListCYTraceCommands, PageResult<Contract.Domain.ContractDto>>
    {
        private readonly IRepository<Contract.Domain.Contract> _repository;
        private readonly Cross_services_Helper cross_Services_Helper;

        public ListCYTraceCommandHandler(IRepository<Contract.Domain.Contract> repository, Cross_services_Helper cross_Services_Helper)
        {
            _repository = repository;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<PageResult<Contract.Domain.ContractDto>> Handle(ListCYTraceCommands request, CancellationToken cancellationToken)
        {
            PageResult<Contract.Domain.ContractDto> pageResult = new PageResult<Contract.Domain.ContractDto>();
            var list = await _repository.GetAllAsync();
            var res = await cross_Services_Helper.Get($"http://10.31.60.9:8000/LongLine/api/LineInfo/GetLineRoute");
            var line = JsonConvert.DeserializeObject<List<LineDto>>(res);
            var info = from A in list.ToList()
                        join B in line on A.Route equals B.Id
                        where A.IsDel == false && A.ContractType == 2 && A.AuditState == 2
                        select new ContractDto
                        {
                            ContractId = A.ContractId,
                            ContractNumber = A.ContractNumber,
                            ContractTitle = A.ContractTitle,
                            ShipperEnterprise = A.ShipperEnterprise,
                            ShipperPrincipal = A.ShipperPrincipal,
                            Route = A.Route,
                            RouteName = B.Name,
                            TonTransport = A.TonTransport,
                            Tonnage = A.Tonnage,
                            CharterAmount = A.CharterAmount,
                            SigningDate = A.SigningDate,
                            Responsible = A.Responsible,
                            CreateDate = A.CreateDate,
                            AuditState = A.AuditState,
                            Approver = A.Approver,
                            Abnormity = A.Abnormity,
                            FreightStatus = A.FreightStatus,
                        };
            if (!string.IsNullOrEmpty(request.ContractTitle))
            {
                info = info.Where(x => x.ContractTitle.Contains(request.ContractTitle));
            }
            if (!string.IsNullOrEmpty(request.ShipperEnterprise))
            {
                info = info.Where(x => x.ShipperEnterprise.Contains(request.ShipperEnterprise));
            }
            if (!string.IsNullOrEmpty(request.ShipperPrincipal))
            {
                info = info.Where(x => x.ShipperPrincipal.Contains(request.ShipperPrincipal));
            }
            if (!string.IsNullOrEmpty(request.Responsible))
            {
                info = info.Where(x => x.Responsible.Contains(request.Responsible));
            }
            if (request.SigningDate != null)
            {
                info = info.Where(x => x.SigningDate.Equals(request.SigningDate));
            }
            int totalCount = info.Count();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            var lists = info.OrderByDescending(x => x.CreateDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            pageResult.List = lists.ToList();
            pageResult.PageCount = totalPage;
            pageResult.TotalCount = totalCount;
            return pageResult;
        }
    }
}
